import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
let car = JSON.parse(localStorage.getItem('car') || '[]')
export default new Vuex.Store({
  state: {
    car: car
  },
  mutations: {
    gotesfot(state, goodshop) {
      let fgae = false
      state.car.some(item => {
        if (item.id == goodshop.id) {
          item.nobuer += parseInt(goodshop.nobuer)
          fgae = true
          return true
        }
      })
      if (!fgae) {
        state.car.push(goodshop)
      }
      localStorage.setItem('car', JSON.stringify(state.car))

    },
    getgoods(state, goodshop) {
      state.car.some(item => {
        if (item.id == goodshop.id) {
          item.nobuer = parseInt(goodshop.nobuer)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    fvcgetid(state, id) {
      state.car.some((item, i) => {
        if (item.id == id) {
          state.car.splice(i, 1)
          return true
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    },
    googote(state, info) {
      state.car.some(item => {
        if (item.id == info.id) {
          item.selected = info.selected
        }
      })
      localStorage.setItem('car', JSON.stringify(state.car))
    }
  },
  getters: {
    geterste(state) {
      let c = 0;
      state.car.forEach(item => {
        c += item.nobuer
      })
      return c
    },
    getnomber(state) {
      let o = {}
      state.car.forEach(item => {
        o[item.id] = item.nobuer
      })
      return o
    },
    gotselect(state) {
      let o = {}
      state.car.forEach(item => {
        o[item.id] = item.selected
      })
      return o
    },
    getpacetate(state) {
      let o = {
        vf: 0,
        tg: 0
      }
      state.car.forEach(item => {
        if (item.selected) {
          o.vf += item.nobuer
          o.tg += item.nobuer * item.price
        }
      })
      return o
    }
  }

})
